package com.hzvdvmc.autosale.seriaport;

import android.os.CountDownTimer;
import android.text.TextUtils;

import com.hzvdvmc.autosale.constant.CmdEnum;
import com.hzvdvmc.autosale.constant.TimeConstant;
import com.hzvdvmc.autosale.socket.event.CommonEvent;
import com.hzvdvmc.autosale.util.LogUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by william on 2018/1/24.
 * 串口执行者
 * 注意：不得多个该对象同时执行
 */

public class SerialClientManager {
    private SeriaCmdRequestManager requester = SeriaCmdRequestManager.getInstacne();
    private SerialCmdResponseManager responser = SerialCmdResponseManager.getInstance();

    private SerialCallback callback;
    private SerialCmdEnum cmd;
    private String data;
    private boolean isReceived = false;
    private boolean isTimeOut = false;
    private String tuneNO;
    private String orderNO;

    public void setTuneNO(String tuneNO) {
        this.tuneNO = tuneNO;
    }

    public void setOrderNO(String orderNO) {
        this.orderNO = orderNO;
    }

    private CountDownTimer timer;

    public SerialClientManager setCallback(SerialCmdEnum cmd, SerialCallback callback) {
        this.cmd = cmd;
        this.callback = callback;
        return this;
    }

    public SerialClientManager setData(String data) {
        this.data = data;
        return this;
    }

    public void execute() {
        responser.setReqeustCmdValue(cmd.getValue());
        responser.setResopnseCallback(new SerialCmdResponseManager.ResopnseCallback() {
            @Override
            public void onCallBack(String cmdValue, String data) {
                LogUtil.i("{SerialClientManager.ResponseCallback}[onCallBack]");
                if (isReceived || isTimeOut) {
                    return;
                }

                LogUtil.i(">>{SerialClientManager}received callback cmdvalue=" + cmdValue + ",data=" + data);
                if (cmdValue.equals(cmd.getValue()) && callback != null) {
                    LogUtil.i(">>{SerialClientManager}--callback");
                    isReceived = true;
                    callback.onSuccess(cmdValue, data,tuneNO,orderNO);
                }
                stopTimeOutTask();
            }
        });

        requester.request(cmd, data);
        startTimeOutTask();
    }

    /**
     * 停止查询任务
     * 在多次创建对象时调用，把之前的停掉
     */
    public void stopTimeOutTask() {
        if (timer != null) {
            timer.cancel();
        }
    }

    /**
     *开始超时计时
     */
    private void startTimeOutTask() {
        LogUtil.i("{SerialClientManager}[startTimeOutTask]开始超时监听");
        stopTimeOutTask();
        timer = new CountDownTimer(TimeConstant.SERIAL_TIMEOUT, TimeConstant.SERIAL_TIMEOUT_INTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                LogUtil.i("{SerialClientManager->CountDownTimer}[onTick]");
                if (isReceived) {
                    timer.cancel();
                } else {
                    requester.request(cmd, data);
                }
            }

            @Override
            public void onFinish() {
                LogUtil.i("{SerialClientManager->CountDownTimer}[onFinish]");
                if (!isReceived) {
                    isTimeOut = true;
                    if (callback != null) {
                        LogUtil.i("serialport", "串口超时");
                        callback.onFile("串口超时",tuneNO,orderNO);
                    }
                }
            }
        };
        timer.start();
    }

//    /**
//     *
//     */
//    private void startTimeOutTask() {
//        LogUtil.i("serialport", "开始超时监听......startTimeOutTask");
//        if (timer != null) {
//            timer.cancel();
//        }
//        timer = new CountDownTimer(1000, 1000) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//
//            }
//
//            @Override
//            public void onFinish() {
//                LogUtil.i("serialport", "startTimeOutTask onFinish");
//                if (!isReceived) {
//                    isTimeOut = true;
//                    EventBus.getDefault().post(new CommonEvent(CommonEvent.SERIL_OUT_TIME));
//                    if (callback != null) {
//                        LogUtil.i("serialport", "串口超时");
//                        callback.onFile("串口超时");
//                    }
//                }
//            }
//        };
//        timer.start();
//    }

}
